package com.community.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.common.mapper.*;
import com.community.common.pojo.Constant;
import com.community.common.pojo.PageRequest;
import com.community.common.pojo.domain.Camera;
import com.community.common.pojo.domain.PublicUser;
import com.community.common.pojo.domain.UserPermission;
import com.community.common.pojo.domain.Visitor;
import com.community.common.pojo.dto.VisitorApplyDto;
import com.community.common.pojo.dto.VisitorCondition;
import com.community.common.pojo.dto.VisitorReceiveDto;
import com.community.common.pojo.response.CodeEnum;
import com.community.common.pojo.response.MyResponse;
import com.community.common.pojo.vo.OwnerCommunityVo;
import com.community.common.pojo.vo.VisitorVo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 访客记录表 服务实现类
 * </p>
 *
 * @author superman
 * @since 2020-08-02
 */
@Service
public class VisitorService extends ServiceImpl<VisitorMapper, Visitor> {

    @Resource
    private VisitorMapper visitorMapper;
    @Resource
    private PublicUserMapper publicUserMapper;
    @Resource
    private UserPermissionMapper userPermissionMapper;
    @Resource
    private OwnerHouseMapper ownerHouseMapper;
    @Resource
    private CameraMapper cameraMapper;

    public List<VisitorVo> queryByPage(PageRequest<VisitorCondition> pageRequest, String communityId) {
        VisitorCondition condition = pageRequest.getCondition();
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        return visitorMapper.queryByPage(condition, communityId);
    }

    public boolean applyVisit(VisitorApplyDto visitorApplyDto) {
        Visitor visitor = Visitor.builder()
                .communityId(visitorApplyDto.getCommunityId())
                .visitedOpenId(visitorApplyDto.getOwnerOpenId())
                .visitorOpenId(visitorApplyDto.getVisitorOpenId())
                .requestStatus(Constant.VISITOR_INIT)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        int insert = visitorMapper.insert(visitor);
        return insert == 1;
    }

    public MyResponse receiveVisit(VisitorReceiveDto visitorReceiveDto) {
        Visitor visitor = visitorMapper.selectById(visitorReceiveDto.getId());
        if (!StringUtils.equals(visitor.getVisitedOpenId(), visitorReceiveDto.getOperatorOpenId())) {
            return MyResponse.fail(CodeEnum.ABNORMAL_BELONGING);
        }
        if (visitorReceiveDto.isReceive()) {
            visitor.setRequestStatus(Constant.VISITOR_SUCCESS);
            String visitorOpenId = visitor.getVisitorOpenId();
            String communityId = visitor.getCommunityId();
            LambdaQueryWrapper<PublicUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PublicUser::getOpenId, visitorOpenId);
            PublicUser publicUser = publicUserMapper.selectOne(queryWrapper);
            LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Camera::getCommunityId, communityId);
            List<Camera> cameras = cameraMapper.selectList(wrapper);
            for (Camera camera : cameras) {
                LambdaQueryWrapper<UserPermission> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(UserPermission::getRelevanceId, publicUser.getId()).eq(UserPermission::getCameraId, camera.getId());
                UserPermission userPermission1 = userPermissionMapper.selectOne(wrapper1);
                if (userPermission1 == null) {
                    UserPermission userPermission = UserPermission.builder()
                            .relevanceId(publicUser.getId())
                            .idTable(1)
                            .communityId(String.valueOf(communityId))
                            .cameraId(camera.getId())
                            .currentAllow(0)
                            .targetAllow(1)
                            .commandStatus(Constant.COMMAND_INIT)
                            .startTime(LocalDateTime.now())
                            .endTime(LocalDateTime.now().plusDays(2))
                            .createTime(LocalDateTime.now())
                            .updateTime(LocalDateTime.now())
                            .build();
                    userPermissionMapper.insert(userPermission);
                } else {
                    userPermission1.setCurrentAllow(0).setTargetAllow(1).setCommandStatus(Constant.COMMAND_INIT)
                            .setStartTime(LocalDateTime.now()).setEndTime(LocalDateTime.now().plusDays(2))
                            .setUpdateTime(LocalDateTime.now());
                    userPermissionMapper.updateById(userPermission1);
                }
            }
        } else {
            visitor.setRequestStatus(Constant.VISITOR_FAIL);
        }
        visitor.setUpdateTime(LocalDateTime.now());
        visitorMapper.updateById(visitor);
        return MyResponse.success();
    }

    public List<VisitorVo> ownerVisitedHistory(PageRequest<VisitorApplyDto> pageRequest) {
        VisitorApplyDto visitorApplyDto = pageRequest.getCondition();
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        return visitorMapper.ownerVisitedHistory(visitorApplyDto);
    }

    public List<VisitorVo> ownerCheckPending(PageRequest<VisitorApplyDto> pageRequest) {
        VisitorApplyDto visitorApplyDto = pageRequest.getCondition();
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        return visitorMapper.ownerCheckPending(visitorApplyDto);
    }

    public List<VisitorVo> visitorVisitedList(PageRequest<VisitorApplyDto> pageRequest) {
        VisitorApplyDto visitorApplyDto = pageRequest.getCondition();
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        return visitorMapper.ownerVisitedHistory(visitorApplyDto);
    }

}
